<?php

use app\model\CouponBrand;
use app\model\PageSetting;
use think\migration\Migrator;


class AlterQueryChangeBrandSettingDir extends Migrator
{
   public function up()
   {
       //查询涉及的目录
       $imageFileArr = array();
       //品牌表image_url
       CouponBrand::query()->whereLike('image_url', '%dmjvip.com%')
           ->field('id, name, image_url')
           ->select()->each(function (CouponBrand $couponBrand) use(&$imageFileArr) {
               if (!$couponBrand->isEmpty()) {
                   if (!empty($couponBrand->image_url)) {
                       //查询数据
                       $imageArr = explode('.com/', $couponBrand->image_url);
                       array_push($imageFileArr,$imageArr[1]);
                   }
               }
           });
       //icon
       CouponBrand::query()->whereLike('icon', '%dmjvip.com%')
           ->field('id, name, icon')
           ->select()->each(function (CouponBrand $couponBrand) use(&$imageFileArr){
               if (!$couponBrand->isEmpty()) {
                   if (!empty($couponBrand->icon)) {
                       //查询数据
                       $iconArr = explode('.com/', $couponBrand->icon);
                       array_push($imageFileArr,$iconArr[1]);
                   }
               }
           });
       //页面跳转配置
       PageSetting::query()->whereLike('image', '%dmjvip.com%')
           ->field('id, image')
           ->chunk(50, function ($list) use (&$imageFileArr) {
               if (empty($list)) {
                   return false;
               }
               /**
                * @var PageSetting $pageSetting
                */
               foreach ($list as $pageSetting) {
                   if ($pageSetting->isEmpty()) {
                       continue;
                   }
                   if (empty($pageSetting->image)) {
                       continue;
                   }
                   //查询数据
                   $imageArr = explode('.com/', $pageSetting->image);
                   array_push($imageFileArr,$imageArr[1]);
               }
           });
       file_put_contents('old_dir.txt', var_export(array_values(array_unique($imageFileArr)),true));
   }
}
